<template>
  <el-container v-loading="loading">
    <el-row>
      <el-form label-width="120px" label-position="left" size="small">
        <el-form-item label="订单类型">
          <el-radio-group v-model="constraintForm.class_id" fill="#1AB394">
            <el-radio-button label="0">全部</el-radio-button>
            <el-radio-button label="1">帮我买</el-radio-button>
            <el-radio-button label="2">帮我送</el-radio-button>
            <el-radio-button label="3">待排队</el-radio-button>
            <el-radio-button label="4">代驾</el-radio-button>
            <el-radio-button label="5">家政</el-radio-button>
            <el-radio-button label="6">代办</el-radio-button>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="订单状态">
          <el-radio-group v-model="constraintForm.type" fill="#1AB394">
            <el-radio-button label="0">全部</el-radio-button>
            <el-radio-button label="1">待付款</el-radio-button>
            <el-radio-button label="2">代发货</el-radio-button>
            <el-radio-button label="3">已发货待收货</el-radio-button>
            <el-radio-button label="4">已完成</el-radio-button>
            <el-radio-button label="5">用户取消</el-radio-button>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="时间">
          <el-radio-group v-model="constraintForm.timeType" fill="#1AB394" @change="getTimed">
            <el-radio-button label="1">不限</el-radio-button>
            <el-radio-button label="2">近一周</el-radio-button>
            <el-radio-button label="3">近一月</el-radio-button>
            <el-radio-button label="4">近三月</el-radio-button>
            <el-radio-button label="5">自定义</el-radio-button>
          </el-radio-group>

          <el-date-picker
            class="el-date-picker"
            v-model="constraintForm.pay_time"
            type="datetimerange"
            value-format="timestamp"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            align="right"
            v-if="constraintForm.timeType == '5'">
          </el-date-picker>
        </el-form-item>

        <el-form-item label="用户名查询">
          <el-autocomplete
            v-model="constraintForm.uText"
            :fetch-suggestions="querySearchUserAsync"
            placeholder="请选择配送人员"
            @select="handleSelectUser"
            clearable>
            <template slot-scope="{ item }">
              <div>{{ item.nickname }}</div>
            </template>
          </el-autocomplete>
        </el-form-item>

        <el-form-item label="配送员名称查询">
          <el-autocomplete
            v-model="constraintForm.rText"
            :fetch-suggestions="querySearchRunnerAsync"
            placeholder="请选择配送人员"
            @select="handleSelectRunner"
            clearable>
            <template slot-scope="{ item }">
              <div>{{ item.name }}</div>
            </template>
          </el-autocomplete>
        </el-form-item>

        <el-form-item>
          <el-button type="success" @click="constraintSearchData">筛选</el-button>
        </el-form-item>
      </el-form>
    </el-row>

    <el-row>
      <el-button
        icon="fa fa-fw fa-refresh"
        size="mini"
        type="info"
        @click="handleRefresh">
      </el-button>
    </el-row>

    <el-table
      @selection-change="handleSelectionChange"
      :data="tableData"
      tooltip-effect="dark"
      border
      :default-sort="{prop: 'date', order: 'descending'}"
      :row-class-name="tableRowClassName"
      size="small">
      <el-table-column
        type="selection"
        width="55">
      </el-table-column>

      <el-table-column
        prop="order_no"
        label="订单号"
        width="180">
      </el-table-column>

      <el-table-column
        prop="goods"
        label="商品"
        width="180">
      </el-table-column>

      <el-table-column
        prop="start_adds"
        label="开始地址"
        width="250">
      </el-table-column>

      <el-table-column
        prop="target_adds"
        label="目的地址"
        width="250">
      </el-table-column>

    <el-table-column
        prop="runner.name"
        label="跑腿人员"
        width="150">
      </el-table-column>

      <el-table-column
        prop="confirm_code"
        label="收获码"
        width="150">
      </el-table-column>

      <el-table-column
        prop="is_send"
        label="是否接单"
        width="120">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.is_send==0">未接单</el-tag>
          <el-tag v-if="scope.row.is_send==1">已接单</el-tag>
        </template>
      </el-table-column>

      <el-table-column
        prop="class_id"
        label="订单类型"
        width="120">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.class_id==1">帮我买</el-tag>
          <el-tag v-if="scope.row.class_id==2">帮我送</el-tag>
          <el-tag v-if="scope.row.class_id==3">待排队</el-tag>
          <el-tag v-if="scope.row.class_id==4">代驾</el-tag>
          <el-tag v-if="scope.row.class_id==5">家政</el-tag>
          <el-tag v-if="scope.row.class_id==6">代办</el-tag>
        </template>
      </el-table-column>

      <el-table-column
        prop="total_price"
        label="总价"
        width="120">
        <template slot-scope="scope">
          <i class="fa fa-fw fa-rmb"></i>
          <span style="margin-left: 10px">{{ scope.row.total_price }}</span>
        </template>
      </el-table-column>

      <el-table-column
        prop="pay_price"
        label="实际支付"
        width="120">
        <template slot-scope="scope">
          <i class="fa fa-fw fa-rmb"></i>
          <span style="margin-left: 10px">{{ scope.row.pay_price }}</span>
        </template>
      </el-table-column>

      <el-table-column
        prop="pay_time"
        label="支付时间"
        width="180">
        <template slot-scope="scope">
          <i class="fa fa-clock-o fa-fw"></i>
          <span style="margin-left: 10px">{{ scope.row.pay_time }}</span>
        </template>
      </el-table-column>

      <el-table-column
        prop="expect_time"
        label="期望时间"
        width="180">
        <template slot-scope="scope">
          <i class="fa fa-clock-o fa-fw"></i>
          <span style="margin-left: 10px">{{ scope.row.expect_time }}</span>
        </template>
      </el-table-column>

      <el-table-column
        prop="user.nickname"
        label="下单用户昵称"
        width="120">
      </el-table-column>

      <el-table-column
        prop="user.member_grade"
        label="下单用户等级"
        width="120">
        <template slot-scope="scope">
          <el-tag>{{scope.row.user.member_grade}}</el-tag>
        </template>
      </el-table-column>

      <el-table-column
        prop="name"
        label="收货人姓名"
        width="120">
      </el-table-column>

      <el-table-column
        prop="phone"
        label="收货人电话"
        width="120">
      </el-table-column>

      <el-table-column
        prop="remark"
        label="备注"
        width="180"
        show-overflow-tooltip>
      </el-table-column>

    </el-table>

    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pagination.currentPage"
      :page-sizes="pagination.pageSizes"
      :page-size="pagination.pageSize"
      :total="pagination.total"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>

  </el-container>
</template>

<script>
  export default {
    mounted: function () {
      this.getData();
    },
    methods: {
      /**
       * 数据请求
       * */
      getData: function () {
        let _this = this;
        this.api.getOrderList({
          page: this.pagination.currentPage,
          paginate: this.pagination.pageSize,
          uid: this.constraintForm.uText != '' ? this.constraintForm.uid : '',
          rid: this.constraintForm.rText != '' ? this.constraintForm.rid : '',
          class_id: this.constraintForm.class_id,
          type: this.constraintForm.type,
          pay_time: this.constraintForm.pay_time.length > 0 ? this.constraintForm.pay_time[0] + '-' + this.constraintForm.pay_time[1] : 0
        }).then(function (res) {
          if (res.code == 1) {
            _this.tableData = res.data.data;
            _this.pagination.total = res.data.total;
          }
          _this.loading = false;
        }).catch(function (res) {
          throw new Error(res);
        });
      },
      /**
       * 分页数改变
       */
      handleSizeChange: function (pageSize) {
        this.pagination.pageSize = pageSize;
        this.getData();
      },
      /**
       * 当前页面改变
       * @param currentPage
       */
      handleCurrentChange: function (currentPage) {
        this.pagination.currentPage = currentPage;
        this.getData();
      },
      tableRowClassName({row, rowIndex}) {
        if (rowIndex === 1) {
          return 'warning-row'
        } else if (rowIndex === 3) {
          return 'success-row'
        }
        return ''
      },
      /**
       * 编辑单个
       * */
      taskAppointItem(index, row) {
        let _this = this;
        let _id = row.id;
        if(_this.constraintForm.text == "" || _this.constraintForm.id == ''){
          this.$message({
            type: 'warning',
            message: '请选在上方进行筛选需要派单的服务人员'
          })
          return;
        }
        this.$confirm('此操作将订单分派给: '+ _this.constraintForm.text +', 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          _this.api.taskAppoint({
            oid: _id,
            rid: _this.constraintForm.id
          }).then(function (res) {
            if (res.code == 1) {
              _this.getData();
              _this.$message({
                showClose: true,
                message: res.msg,
                type: 'success',
              });
            } else {
              _this.$message({
                showClose: true,
                message: res.msg,
                type: 'warning'
              });
            }
          }).catch(function (res) {
            throw new Error(res);
          });
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消该操作'
          })
        })
      },
      /**
       * 数据刷新
       */
      handleRefresh: function () {
        this.getData();
        this.$message({
          showClose: true,
          message: '刷新成功',
          type: 'success'
        });
      },
      /**
       * 表格选中状态记录
       * @param val
       */
      handleSelectionChange(val) {
        this.multipleSelection = val;
      },
      /**
       * 获取时间
       * @param type
       */
      getTimed: function () {
        const start = new Date();
        const end = new Date();
        let day = 0;
        if (this.constraintForm.timeType == '2') {
          day = 7;
        } else if (this.constraintForm.timeType == '3') {
          day = 30;
        } else if (this.constraintForm.timeType == '4') {
          day = 90;
        }
        if (this.constraintForm.timeType == '2' || this.constraintForm.timeType == '3' || this.constraintForm.timeType == '4') {
          start.setTime(start.getTime() - 3600 * 1000 * 24 * day);
          let timeArr = [];
          timeArr.push(start.getTime());
          timeArr.push(end.getTime());
          this.constraintForm.pay_time = timeArr;
        } else if (this.constraintForm.timeType == '1' || this.constraintForm.timeType == '5') {
          this.constraintForm.pay_time = [];
        }
      },
      /**
       * 条件数据检索
       */
      constraintSearchData: function () {
        this.getData();
        this.$message({
          showClose: true,
          message: '查询成功',
          type: 'success'
        });
      },
      /**
       * 远程数据加载用户
       */
      querySearchUserAsync(queryString, cb) {
        let _this = this;
        this.api.searchUserByUserName({name: queryString}).then(function (res) {
          if(res.code == '1'){
            cb(res.data.data);
          }
        }).catch(function (res) {
          throw new Error(res);
        });
      },
      /**
       * 搜索下拉选择用户
       * @param item
       */
      handleSelectUser(item) {
        this.constraintForm.uText = item.nickname;
        this.constraintForm.uid = item.id;
      },
      /**
       * 远程数据加载跑腿
       */
      querySearchRunnerAsync(queryString, cb) {
        let _this = this;
        this.api.searchRunnerByUserName({name: queryString}).then(function (res) {
          if(res.code == '1'){
            cb(res.data.data);
          }
        }).catch(function (res) {
          throw new Error(res);
        });
      },
      /**
       * 搜索下拉选择跑腿
       * @param item
       */
      handleSelectRunner(item) {
        this.constraintForm.rText = item.name;
        this.constraintForm.rid = item.id;
      },
    },
    data() {
      return {
        loading: true,  // 页面加载
        tableData: [
          // {
          //   "id":16,                //类型：Number  必有字段  备注：订单id
          //   "name":"熊二",                //类型：String  必有字段  备注：姓名
          //   "phone":"15111111111",                //类型：String  必有字段  备注：电话
          //   "uid":18,                //类型：Number  必有字段  备注：关联用户
          //   "rid":0,                //类型：Number  必有字段  备注：跑腿id
          // "runner":{                //类型：Object  必有字段  备注：跑腿
          //      "name":"帅哥"                //类型：String  必有字段  备注：跑腿人员名
          //  },
          // "confirm_code":"mock",                //类型：String  必有字段  备注：收货码
          //   "order_no":"2018090319111523994666",                //类型：String  必有字段  备注：订单号
          //   "pic":"mock",                //类型：String  必有字段  备注：图片路径
          //   "pics":"mixed",                //类型：Mixed  必有字段  备注：图片合集
          //   "voice":"mock",                //类型：String  必有字段  备注：语音路径
          //   "goods":"美食",                //类型：String  必有字段  备注：商品名
          //   "class_id":1,                //类型：Number  必有字段  备注：1帮我买2帮我送3带排队4代驾5家政6代办
          //   "total_price":"34.86",                //类型：String  必有字段  备注：订单总费用
          //   "pay_price":"32.22",                //类型：String  必有字段  备注：实际支付费用
          //   "weight":0,                //类型：Number  必有字段  备注：重量
          //   "start_adds":"陕西省西安市未央区未央路60号qweqw",                //类型：String  必有字段  备注：起始位置
          //   "start_adds_location":{                //类型：Object  必有字段  备注：起始经纬度
          //     "lng":108.95323064451,                //类型：Number  必有字段  备注：无
          //     "lat":34.301103430993                //类型：Number  必有字段  备注：无
          //   },
          //   "target_adds":"陕西省西安市未央区凤城八路",                //类型：String  必有字段  备注：目标位置
          //   "target_adds_location":{                //类型：Object  必有字段  备注：目标坐标
          //     "lng":108.95323064451,                //类型：Number  必有字段  备注：无
          //     "lat":34.301103430993                //类型：Number  必有字段  备注：无
          //   },
          //   "remark":"adaqeq",                //类型：String  必有字段  备注：备注
          //   "pay_time":"mock",                //类型：String  必有字段  备注：下单时间
          //   "expect_time":"1970-01-01 08:11:06",                //类型：String  必有字段  备注：期待送达时间
          //   "bargaining":1,                //类型：Number  必有字段  备注：是否接收议价0否，1是
          //   "is_service":"mock",                //类型：String  必有字段  备注：确认送达
          //   "service_time":"mock",                //类型：String  必有字段  备注：确认送达时间
          //   "pay_type":"mock",                //类型：String  必有字段  备注：支付方式1是微信2是余额
          //   "apart":"mock",                //类型：String  必有字段  备注：距离
          //   "secure_price":"mock",                //类型：String  必有字段  备注：保证金
          //   "apply_delete":"mock",                //类型：String  必有字段  备注：申请取消订单
          //   "apply_time":"mock",                //类型：String  必有字段  备注：申请取消时间
          //   "is_send":"mock",                //类型：String  必有字段  备注：0未接单，1已接单
          //   "send_time":"mock",                //类型：String  必有字段  备注：接单时间
          //   "reward_price":"mock",                //类型：String  必有字段  备注：跑腿人员红包
          //   "user":{                //类型：Object  必有字段  备注：关联用户
          //     "id":18,                //类型：Number  必有字段  备注：uid
          //     "nickname":"在水一方",                //类型：String  必有字段  备注：昵称
          //     "member_grade":2                //类型：Number  必有字段  备注：等级
          //   }
          // },
        ],
        multipleSelection: [],
        pagination: {
          total: 5,
          currentPage: 1,
          pageSize: 10,
          pageSizes: [5, 10, 20, 25, 30, 50, 100],
        },
        constraintForm: {
          uid:'',		// 	用户id，默认0，会员用户-》用户列表-》获取用户名查询列表
          uText:'', //  用户名称字符
          rid:'',		// 	跑腿id，默认0，配送员-》配送员评价-》获取配送员名称模糊查询列表
          rText:'', //  配送员名称字符
          class_id:'0',	// 1帮我买2帮我送3带排队4代驾5家政6代办，默认0
          type:'0',	// 	1：待付款 2：代发货 3：已发货待收货 4：已完成 5：用户取消 ,默认0
          pay_time:'',	// 下单时间默认0，'-'分隔时间戳，没有毫秒
          timeType: '1',   // 时间类型 1=>不限 2=>近一周 3=>近一月 4=>近三月 5=>自定义
        }
      }
    },
  }
</script>

<style scoped>
  .el-container {
    display: block;
  }

  .el-table, .el-row {
    width: 98% !important;
    margin: 15px auto;
  }

  .el-pagination {
    text-align: center;
    margin: 15px 0;
  }

  .el-table .success-row {
    background: #f0f9eb;
  }

  .logo-img {
    width: 40px;
    height: 40px;
  }

  .autocomplete-group{
    display: inline-block;
    margin-left: 15px;
    font-size: 12px;
  }
</style>
